import axios from 'axios'
export default{
  namespaced:true,
  state:{
    list:[]
  },
  mutations:{
    updateList(store,list){
      store.list = list
    },
    updatestate(store,{id,goods_state}){
      const item= store.list.find(item=>item.goods_id===id)
      item.goods_state = goods_state
    },
    updateAllState(store,falg){
      store.list.forEach(item=>item.goods_state=falg)
    },
    updateCount(store,{id,num}){
    const item =  store.list.find(item=>item.goods_id===id)
    item.goods_count =num
    }
  },
  actions:{
   async getList(state){
      const res = await axios.get('https://applet-base-api-t.itheima.net/api/cart')
      console.log(res);
      state.commit('updateList',res.data.list)
    }
  },
  getters:{
    totalState(store){
      return store.list.every(item=>item.goods_state)
    },
    totalCount(store){
      let count = 0
       store.list.forEach(item=>{
        if(item.goods_state){
          count += item.goods_count
        }
      })
      return count
    },
    totalPrice(store){
      let price= 0

       store.list.forEach(item=>{
        if(item.goods_state){
         price += item.goods_price *item.goods_count
        }
      })
      return price
    }
  }
}